package threading;


import gui.GUIUtils;
import gui.main.MainPanelGUIView;

import java.util.LinkedList;
import java.util.List;

import objects.ObjectCodes;
import objects.MessageCodes;
import objects.data.ClusterData;
import objects.data.Message;
import objects.predictors.Co2Data;
import objects.predictors.QueueTimeData;
import objects.predictors.ResourceAvailabilityData;

import actions.UpdateHosts;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;



import threading.runnable.FileOperations;
import utils.Utils;
import web.Hosts;
import web.WebSocket;

public class ReceiveThread extends Thread implements ObjectCodes, MessageCodes {
	public static volatile boolean run = true;
	private WebSocket ws;
	private static MainPanelGUIView mainPanel = GUIUtils.MainPanel;
	public ReceiveThread() {
		this.ws = Hosts.ws;
	}

	public void run() {
		while (run) {
			try {
				String data = ws.recv();
				Utils.out("RECV: " + data);
				String[] tokens = data.split("\\|");
				byte obj_type = Byte.parseByte(tokens[0]);
				String json = tokens[1];

				if (obj_type == PING) {
					List<ClusterData> cd = new Gson().fromJson(json,
							new TypeToken<LinkedList<ClusterData>>() {
							}.getType());
					Utils.userInfo.cinfo = cd;
					UpdateHosts.comboBoxUpdate();
					ws.send("8|0");
					continue;
				}
				
				switch (obj_type) {
				case ATTR_INFO: {
					QueueTimeData ai = QueueTimeData.fromJSon(json);
					//JobPanelController.updateJobInfo(ai);
					break;
				}
				case RES_INFO: {
					ResourceAvailabilityData ri = ResourceAvailabilityData.fromJSon(json);
					//JobPanelController.updateJobInfo(ri);
					break;
				}
				case CO2_INFO: {
					Co2Data ci = Co2Data.fromJSon(json);
					Utils.out("UPDATING INFO: " + ci);
					//JobPanelController.updateJobInfo(ci);
					break;
				}
				case MSG: {
					Message msg = Message.fromJson(json);
					switch (msg.type) {
					case JOB_QUEUED: {
						//JobPanelController.updateJobStatus(Utils.p, msg.msg,
						//		"Queued");
						break;
					}
					case JOB_EXECUTING: {
						//JobPanelController.updateJobStatus(Utils.p, msg.msg,
						//		"Running");
						break;
					}
					case JOB_FAILED: {
						//JobPanelController.updateJobStatus(Utils.p, msg.msg,
						//		"Failed");
						break;
					}
					case JOB_TERMINATED: {
						Utils.exec.execute(new FileOperations(msg.msg,0));
						break;
					}
					case CLUSTER_ON: {
						/*if (!CluPanelController.clusters.contains(msg.msg)) {
							if (CluPanelController.clusters.isEmpty())
								Utils.p.cluCombo.removeAllItems();
							Utils.p.cluCombo.addItem(msg.toHost);
						}
						CluPanelController.addClusterInfo(Utils.p, msg.msg);
						Utils.userInfo.cinfo.add(new ClusterIDInfo(msg.msg,
								msg.toHost));*/
						break;
					}
					case CLUSTER_OFF: {
						/*CluPanelController.updateClusterStatus(Utils.p,
								msg.msg, "Disconnected");
						int i = 0;

						while (i < Utils.p.cluCombo.getItemCount()) {
							String item = (String) Utils.p.cluCombo
									.getItemAt(i);
							if (item.equals(msg.toHost)) {
								Utils.p.cluCombo.removeItemAt(i);
								break;
							}
							i++;
						}
						Utils.p.cluCombo.setSelectedIndex(0);*/
						break;
					}
					}
					break;
				}
				}

			} catch (Exception e) {
				e.printStackTrace();
				// System.out.println(e.getMessage());
				// run = false;
			}

		}

	}
}
